/**
 * 
 */
$(function(){
	   $('#adorder_table')
		.datagrid(
				{
					url : 'admin/queryAdOrderInfo.do?pageSize=10',
					collapsible : true,
					width : '100%',
					height :'450px',
					iconCls : 'icon-user',
					fitColumns : true,
					pagination : true,
					rownumbers : true,
					singleSelect:true,
					checkOnSelect:false,
					selectOnCheck:false,
					loadMsg:'正在加载数据',
					loadFilter:function(data){
						for(var i=0;i<data.rows.length;i++){
							if(data.rows[i].orderPayStatus=='1'){
								data.rows[i].orderPayStatus='已支付';
							}else{
								data.rows[i].orderPayStatus='未支付';
							}
					}
						 return data;
					},
					onClickRow: function(rowIndex, rowData) {
						//layer.msg("选中");
						
					},
					onDblClickRow :function(rowIndex,rowData){
						//layer.msg("double click");
					  },
					toolbar:'#toolbar',
					columns : [ [
							{
								field : 'projectTitle',
								title : '项目名',
								width : 50,
								align : 'center'
							}, 
							{
								field : 'itemTitle',
								title : '回报名',
								width : 50,
								align : 'center'
							},
							{
								field : 'orderPrice',
								title : '订单价格',
								width : 50,
								align : 'center'
							},
							{
								field : 'orderPayStatus',
								title : '支付状态',
								width : 50,
								align : 'center'
							},
							{
								field : 'orderPayMethod',
								title : '支付方式',
								width : 50,
								align : 'center'
							},
							{
								field : 'orderPayNumber',
								title : '支付流水号',
								width : 50,
								align : 'center'
							},
							{
								field : 'orderCreateTime',
								title : '订单创建时间',
								width : 50,
								align : 'center'
							},{
								field : 'userName',
								title : '支付人',
								width : 50,
								align : 'center'
							},{
								field : 'userTelephone',
								title : '支付人电话',
								width : 50,
								align : 'center'
							},
							{
								field : 'orderReceiverName',
								title : '收货人',
								width : 50,
								align : 'center'
							},
							{
								field : 'orderReceiverTelephone',
								title : '收货电话',
								width : 50,
								align : 'center'
							},
							{
								field : 'orderReceiverAddress',
								title : '收货地址',
								width : 50,
								align : 'center'
							},
							{
								field : 'orderDescription',
								title : '订单备注',
								width : 50,
								align : 'center'
							}
						   ] ]
				});
	 //设置分页控件  
		
	var p = $('#adorder_table').datagrid('getPager');
		$(p).pagination({
			pageSize : 10,//每页显示的记录条数，默认为10  
			pageList : [ 5, 10, 20, 30, 40, 50 ],//可以设置每页记录条数的列表  
			beforePageText : '第',//页数文本框前显示的汉字  
			afterPageText : '页    共 {pages} 页',
			displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
			onSelectPage : function(pageNumber, pageSize) {
				//layer.msg("pageNumber="+pageNumber+"pageSize="+pageSize)
				var datas = {
					orderPayStatus:$("#sorderPayStatus").combobox('getValue'),
					orderReceiverName:$("#sorderReceiverName").textbox('getValue'),
					projectTitle:$("#sprojectTitle").textbox('getValue'),
					itemTitle:$("#sitemTitle").textbox('getValue'),
					currentPage : (pageNumber - 1) * pageSize,
					pageSize : pageSize
				}
				loadAdorderData(datas);
			},
			onRefresh : function(pageNumber, pageSize) {
				var datas = {
						orderPayStatus:$("#sorderPayStatus").combobox('getValue'),
						orderReceiverName:$("#sorderReceiverName").textbox('getValue'),
						projectTitle:$("#sprojectTitle").textbox('getValue'),
						itemTitle:$("#sitemTitle").textbox('getValue'),
						currentPage : (pageNumber - 1) * pageSize,
						pageSize : pageSize
					}
					loadAdorderData(datas);
			}
		});
	});
	//delete btn listener
	function deleteAdorder(){
		layer.confirm('是否确定删除？', {
			  btn: ['确定','点错了'] //按钮
			}, function(){
				$.ajax({
					url : $("#saveUrl").val(),
					type : "POST",
					dataType : "json",
					data:{},
					success : function(result) {
						if (result.result == 0) {
							layer.msg(result.tips);
						} else {
							layer.msg(result.tips);
							$('#adorder_table').datagrid('reload'); // reload the data
						}
					}
			});
			}, function(){
			  layer.msg("下次小心点!");
			});
	}
	
	$(function(){
		$('#update').bind('click',function(){
			var row = $('#adorder_table').datagrid('getSelected');
			if(row){
				$('#adorderfm').form('clear');
				$('#adorderdlg').dialog('open').dialog("setTitle","修改收货人信息");
				$('#adorderfm').form('load',row);
				$("#saveUrl").val('admin/updateAdorderInfo.do?orderId='+row.orderId);
			}else{
				layer.msg("请选中一行!");
			}
		});

		$('#delete').bind('click',function(){
			var row = $('#adorder_table').datagrid('getSelected');
			 if(row){
				 if(row.orderPayStatus=='未支付'){
					 $("#saveUrl").val('admin/deleteAdOrderInfo.do?orderId='+row.orderId);
					 deleteAdorder();
				 }else{
					 layer.msg('订单已支付，禁止删除!');
				 }
			 }else{
				 layer.msg("请选中一行!");
			 }
		});
		$('#export').bind('click',function(){
			exportExcel();
		});
	});
	//save btn
	function adorderSave(){
		var pageSize = $('#adorder_table').datagrid('getPager').data("pagination").options.pageSize;
		var pageNumber = $('#adorder_table').datagrid('getPager').data("pagination").options.pageNumber;
		var datas={
				currentPage : (pageNumber - 1) * pageSize,
				pageSize:pageSize
				};
		$.ajax({
			url : $("#saveUrl").val(),
			type : "POST",
			dataType : "json",
			data : {
				orderReceiverName:$("#orderReceiverName").textbox('getValue'),
				orderReceiverTelephone:$("#orderReceiverTelephone").textbox('getValue'),
				orderReceiverAddress:$("#orderReceiverAddress").textbox('getValue'),
			},
			beforeSend: function(){
				var flag=$("#adorderfm").form('enableValidation').form('validate');
				if(flag){
				$('#save').linkbutton({disabled:true}); 
				 return flag;
				}else{
					return flag;
				}
		       
	         },
			success : function(result) {
				$('#save').linkbutton({disabled:false}); 
				if (result.result == 0) {
					layer.msg(result.tips);
				} else {
					layer.msg(result.tips);
					$('#adorderdlg').dialog('close'); // close the dialog
					$('#adorder_table').datagrid('reload',datas); // reload the data
				}
			}
		});
	}
	//cancel btn
	function adorderCancel(){
		$('#save').linkbutton({disabled:false}); 
		$('#adorderfm').form('clear');
		$('#adorderdlg').dialog('close');
	}
	//search btn
	function searchAdorder(){
		var pageSize = $('#adorder_table').datagrid('getPager').data("pagination").options.pageSize;
		var datas={
				pageSize:pageSize,
				orderPayStatus:$("#sorderPayStatus").combobox('getValue'),
				orderReceiverName:$("#sorderReceiverName").textbox('getValue'),
				projectTitle:$("#sprojectTitle").textbox('getValue'),
				itemTitle:$("#sitemTitle").textbox('getValue'),
		}
		loadAdorderData(datas);
	}
	//resetValue btn
	function resetAdorder(){
		$("#sorderPayStatus").combobox('setValue','');
		$("#sorderReceiverName").textbox('setValue','');
		$("#sprojectTitle").textbox('setValue','');
		$("#sitemTitle").textbox('setValue','');
		var pageSize = $('#adorder_table').datagrid('getPager').data("pagination").options.pageSize;
			var datas = {
				pageSize : pageSize,
			}
			loadAdorderData(datas);
	}
	//export excel from database
	function exportExcel(){
		var orderReceiverName=$("#sorderReceiverName").textbox('getValue');
		var projectTitle=$("#sprojectTitle").textbox('getValue');
		var itemTitle=$("#sitemTitle").textbox('getValue');
		var orderPayStatus=$("#sorderPayStatus").combobox('getValue');
		$("#saveUrl").val('admin/exportAdorderInfoByCondition.do?orderReceiverName='
		+orderReceiverName+'&projectTitle='+projectTitle+'&itemTitle='+itemTitle+'&orderPayStatus='+orderPayStatus);
		
		var url=$("#saveUrl").val();
        window.open(url);
	}
	//load datas
	var loadAdorderData=function(datas){
		 $.ajax({
				url : 'admin/queryAdOrderInfo.do',
				type : "POST",
				dataType : "json",						
				data : datas,
				success : function(param) {
						$('#adorder_table').datagrid('loadData',param);
				}
		});  
	}